// import moment from 'momnet'

export default {
  // ('Thu Dec 31 2026 00:00:00 GMT+0800 (中国标准时间)') =>1798646400000
  standardTimeToTimestamp: (time) => {
    return time ? Date.parse(time) : "";
  },

  /**
   * 计算剩余时间
   * @param {number} expireTimestamp 到期时间（时间戳，毫秒）
   * expireTimestamp: 1545790815000
   * 返回：1天20小时19分钟
   */
  getRemainTime: (expireTimestamp) => {
    let duration = expireTimestamp - Date.now();
    const oneDay = 24 * 60 * 60 * 1000;
    const oneHou = 60 * 60 * 1000;
    const oneMin = 60 * 1000;

    let days = 0;
    let hours = 0;
    let minutes = 0;

    let result = "";

    days = parseInt(String(duration / oneDay));
    duration -= days * oneDay;

    hours = parseInt(String(duration / oneHou));
    duration -= hours * oneHou;

    minutes = parseInt(String(duration / oneMin));

    if (days) result += `${days}天`;
    if (hours) result += `${hours}小时`;
    if (minutes) result += `${minutes}分钟`;

    return result;
  },

  /**
   *
   * @param data
   * @return 时间戳
   * @example 2019-10-09T03:10:10.000+00:00=>1570585260000
   * @example 2019-10-09 09:41=>1570585260000
   */
  getTimestamp: (data) => {
    const isSpecial = /[^A-Za-z]/gi.test(data); // true 说明没有字母
    if (isSpecial) {
      return new Date(data).getTime();
    } else {
      const thisTime = data && data.replace(/-/g, "/");
      return new Date(thisTime).getTime();
    }
  },

  // Date_format('YYYY/MM/DD',new Date()) => 2021/03/16
  Date_format: (type, timeStamp) => {
    let stamp = new Date().getTime();
    if (timeStamp) stamp = timeStamp;
    const dateTime = new Date(stamp);
    const year: number | string = dateTime.getFullYear();
    let month: number | string = dateTime.getMonth() + 1;
    let date: number | string = dateTime.getDate();
    const day: number | string = dateTime.getDay();
    let hour: number | string = dateTime.getHours();
    let minutes: number | string = dateTime.getMinutes();
    let sec: number | string = dateTime.getSeconds();
    const arrWeek = [
      "星期日",
      "星期一",
      "星期二",
      "星期三",
      "星期四",
      "星期五",
      "星期六",
    ];
    const week = arrWeek[day];
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minutes < 10) minutes = "0" + minutes;
    if (sec < 10) sec = "0" + sec;
    let time = "";
    switch (type.toLocaleUpperCase()) {
      case "YYYY/MM/DD":
        time = year + "/" + month + "/" + date;
        break;
      case "YYYY-MM-DD":
        time = year + "-" + month + "-" + date;
        break;
      case "YYYY年MM月DD日":
        time = year + "年" + month + "月" + date + "日";
        break;
      case "YYYY年MM月DD日 WEEK HH:MM:SS":
        time =
          year +
          "年" +
          month +
          "月" +
          date +
          "日" +
          " " +
          week +
          " " +
          hour +
          ":" +
          minutes +
          ":" +
          sec;
        break;
      case "YYYY-MM-DD HH:MM:SS":
        time =
          year +
          "-" +
          month +
          "-" +
          date +
          " " +
          hour +
          ":" +
          minutes +
          ":" +
          sec;
        break;
      case "YYYYMMDDHHMMSS":
        time =
          year + "" + month + "" + date + "" + hour + "" + minutes + "" + sec;
        break;
    }
    return time;
  },
  // 获取两个日期之间的天数间隔
  getDaysDiffBetweenDates: (dateInitial, dateFinal) =>
    (dateFinal - dateInitial) / (1000 * 3600 * 24),

  // Example
  // getDaysDiffBetweenDates(new Date("2017-12-13"), new Date("2017-12-22")); // 9

  getDayOfLastMonth: function (date) {
    const daysInMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    let strYear = date.getFullYear();
    let strDay = date.getDate();
    let strMonth = date.getMonth() + 1;
    if (strYear % 4 === 0 && strYear % 100 !== 0) {
      daysInMonth[2] = 29;
    }
    if (strMonth - 1 === 0) {
      strYear -= 1;
      strMonth = 12;
    } else {
      strMonth -= 1;
    }
    strDay = daysInMonth[strMonth] >= strDay ? strDay : daysInMonth[strMonth];
    if (strMonth < 10) {
      strMonth = "0" + strMonth;
    }
    if (strDay < 10) {
      strDay = "0" + strDay;
    }
    return strYear + "-" + strMonth + "-" + strDay;
  },
  /* 比较两个日期的大小 */
  dateCompare: function (date1, date2) {
    const arr1 = date1.split("-");
    const time1 = new Date(arr1[0], arr1[1] - 1, arr1[2]).getTime();

    let time2;
    if (!date2) {
      time2 = new Date().getTime();
    } else {
      const arr2 = date2.split("-");
      time2 = new Date(arr2[0], arr2[1] - 1, arr2[2]).getTime();
    }

    if (time1 > time2) {
      return ">";
    } else if (time1 === time2) {
      return "=";
    } else {
      return "<";
    }
  },

  dateAdd: function (strInterval, Number, timestamp) {
    const timeStr = timestamp || new Date().getTime();
    const dtTmp: any = new Date(timeStr);
    let result;
    switch (strInterval) {
      case "s": // 秒
        result = new Date(Date.parse(dtTmp) + 1000 * Number);
        break;
      case "n": // 分
        result = new Date(Date.parse(dtTmp) + 60000 * Number);
        break;
      case "h": // 时
        result = new Date(Date.parse(dtTmp) + 3600000 * Number);
        break;
      case "d": // 天
        result = new Date(Date.parse(dtTmp) + 86400000 * Number);
        break;
      case "w": // 周 即7天
        result = new Date(Date.parse(dtTmp) + 86400000 * 7 * Number);
        break;
      case "q": // 季 即3个月
        result = new Date(
          dtTmp.getFullYear(),
          dtTmp.getMonth() + Number * 3,
          dtTmp.getDate(),
          dtTmp.getHours(),
          dtTmp.getMinutes(),
          dtTmp.getSeconds()
        );
        break;
      case "m": // 月
        result = new Date(
          dtTmp.getFullYear(),
          dtTmp.getMonth() + Number,
          dtTmp.getDate(),
          dtTmp.getHours(),
          dtTmp.getMinutes(),
          dtTmp.getSeconds()
        );
        break;
      case "y": // 年
        result = new Date(
          dtTmp.getFullYear() + Number,
          dtTmp.getMonth(),
          dtTmp.getDate(),
          dtTmp.getHours(),
          dtTmp.getMinutes(),
          dtTmp.getSeconds()
        );
        break;
    }
    return result;
  },
};

// 两个时间的差距是否在24小时内
// var isIn24Hours = function (t1, t2) {
//     if (t1 == null || t2 == null) return false;

//     var duration = moment.duration(moment(t1).diff(moment(t2)));
//     var durationHrs = parseInt(Math.abs(duration.asHours()));
//     return durationHrs < 24;
// }

// 返回某天日期
// function datePick(param, format = 'YYYY-MM-DD') {
//     if (typeof param === 'number') param = String(param)
//     switch (param) {
//         case '1':
//             return moment().format(format)
//         case '7':
//             return moment().subtract('days', 6).format(format)
//         case '30':
//             return moment().subtract('days', 29).format(format)
//         case 'month':
//             return moment().startOf('month').format(format)
//         default:
//             return moment().subtract('days', param - 1).format(format)
//     }
// }

// function getLastTime(time) {
//     const now = moment();
//     let last = moment(time);
//     last = last.add(1, "days");
//     let diff = last.diff(now, "hours");
//     if (diff > 0) return diff + "小时";
//     if (diff < 0) return "0小时";
//     diff = last.diff(now, "minutes");
//     if (diff < 0) {
//         diff = 0; // 不显示负数
//     }
//     return diff + "分钟";
// }
